package com.java.controller;

import com.java.pojo.EnterGood;
import com.java.pojo.GoodStyle;
import com.java.pojo.Lack;
import com.java.pojo.Page;
import com.java.service.GoodStyleService;
import com.java.service.LackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class LackController {

    @Autowired
    private LackService lackService;

    @Autowired
    private GoodStyleService goodStyleService;

    //进入添加页面
    @RequestMapping(value = "/preAddLack")
    public String addLack(Model model){
        List<GoodStyle> goodStyleList = goodStyleService.getGoodStyleList();
        model.addAttribute("list",goodStyleList);
        return "lack/AddLack";
    }


    @RequestMapping(value = "/addLack")
    @ResponseBody
    public String addLack2(Lack lack){
        int i = lackService.addLack2(lack);
        if(i>0){
            return "<script>alert('ADD Successful');location.href='/findLackList?goodname3=';</script>";
        }else {
            return "<script>alert('ADD Lose');location.href='/findLackList?goodname3=';</script>";
        }
    }


    //查询全部,分页
    @RequestMapping(value = "/findLackList",produces = "text/html;charset=UTF-8")
    public String findLackList(@RequestParam(value = "start",required = false) Integer start, @RequestParam(value = "goodname3",required = false)String goodname3 , Model model){
        if(goodname3==null ||"".equals(goodname3)){
            goodname3="";
        }
        if(start==null ||start.equals("")){
            start=1;
        }
        System.out.println(start+"+"+goodname3);
        /*计算总页数*/
        int totalCount = lackService.getCount(goodname3);
        int i = totalCount % 4 ==0? totalCount/4 :(totalCount/4)+1;
        /*如果最后一页大于总页数,就让其等于总页数,html按钮最后一页
         * 如果前一页到0的时候,把他变成1*/
        if(start>=i){
            start=i;
        }else if(start<=1){
            start=1;
        }
        Page<Lack> page =lackService.getPageByPage(start,goodname3);
        System.out.println("page:"+page);
        if(page!=null&&!page.equals("")){
            model.addAttribute("page",page);
            model.addAttribute("goodname3",goodname3);
            return "lack/findLack";
        }else {
            model.addAttribute("page","");
            return "lack/findLack";
        }
    }


    //进货员收到
    @RequestMapping(value = "/over",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String over(@RequestParam(value = "id",required = false) String id) {
        int i = lackService.updLack(id);
        System.out.println("iii"+i+"oo"+id);
        if (i > 0) {
            return "<script>alert('successful');location.href='/findLackList?goodname3=';</script>";
        } else{
            return "<script>alert('lose');location.href='/findLackList?goodname3=';</script>";
        }
    }

    //删除
    @RequestMapping(value = "/delLack",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String  delLack(@RequestParam(value = "id",required = false) String id) {
        int i = lackService.del(id);
        System.out.println("iii"+i+"oo"+id);
        if (i > 0) {
            return "<script>alert('del successful');location.href='/findLackList?goodname3=';</script>";
        } else{
            return "<script>alert('del lose');location.href='/findLackList?goodname3=';</script>";
        }
    }




}
